<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <TrainTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5" :searchInfo="searchInfo" :rowSelection="rowSelection">
      <template #toolbar>
        <CollapseContainer class="mt-4">
          <a-button type="primary" @click="handleCreate" :disabled="btnState.disableHandleCreate"> 新增贵宾厅 </a-button>
          <a-button type="primary" @click="handleSetViproomServer(selectedRow)" :disabled="!hasSelectedRows">设置服务 </a-button>
          <a-button type="primary" @click="handleSetWeChat(selectedRow)" :disabled="!hasSelectedRows"> 设置上架公众号 </a-button>
          <a-button type="primary" @click="handleLookGoodsLog(selectedRow)" :disabled="!hasSelectedRows"> 查看日志 </a-button>
        </CollapseContainer>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="reload" />
    <ViproomServerSetDrawer @register="registerViproomServerSetDrawer" />
    <GoodsWechatSetDrawer @register="registerViproomWechatSetDrawer" />
    <GoodsLogModal @register="registerGoodsLogModal"></GoodsLogModal>
  </PageWrapper>
</template>
<script lang="ts" setup name="trainViproom">
import { reactive } from 'vue';
import { PageWrapper } from '@apps/antd-admin/src/components/page';
import { BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useModal } from '@apps/antd-admin/src/components/modal';
import TrainTree from '@apps/ant-bus/src/views/business/highspeedtrain/TrainTree.vue';
import ViproomServerSetDrawer from '../components/ViproomServerSetDrawer.vue';
import GoodsWechatSetDrawer from '../components/ViproomWechatSetDrawer.vue';
import GoodsLogModal from '../../goodsLog/GoodsLogModal.vue';
import Drawer from './drawer.vue';
import { columns, searchFormSchema } from '../data';
import { Api } from '../api';

const { createMessage } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerGoodsLogModal, { openModal: openGoodsLogModal }] = useModal();
const [registerViproomServerSetDrawer, { openDrawer: openViproomServerSetDrawer }] = useDrawer();
const [registerViproomWechatSetDrawer, { openDrawer: openViproomWechatSetDrawer }] = useDrawer();
const searchInfo = reactive<Recordable>({ viproomType: 2 });
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'vipromm2-template',
  tableProps: {
    //表格标题
    title: '机场贵宾厅列表',
    //主键字段
    rowKey: 'viproomId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows, selectedRow }] = tableContext;

const btnState = reactive({
  disableHandleCreate: true,
});

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
    highSpeedTrainId: searchInfo.highSpeedTrainId,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleSelect(highSpeedTrainId) {
  btnState.disableHandleCreate = false;
  searchInfo.highSpeedTrainId = highSpeedTrainId;
  console.info('searchInfo', searchInfo);
  reload();
}

function handleSetViproomServer(record: Recordable) {
  openViproomServerSetDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleSetWeChat(record: Recordable) {
  openViproomWechatSetDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleLookGoodsLog(record: Recordable) {
  let data = { goodsId: record.viproomId };
  openGoodsLogModal(true, {
    data,
    isUpdate: true,
  });
}
</script>
